Technical Q&A
QTMCC 13 - Setting Export Quality (06-December-1999)
Q: We currently have a file which we would like to export in a different format. We're using GetGraphicsImporterForFile() and GraphicsImportExportImageFile() to do this. However, we didn't see how to change the compression factor if we're saving as a JPEG file. Is this possible?
A: This is possible with just a little more work than using GraphicsImportExportImageFile() . The idea is to use the Graphics Exporter Component APIs with a specific call to GraphicsExportSetCompressionQuality() or GraphicsExportSetTargetDataSize() to set the compression quality for the export.
Here's an overview of the steps to follow:
- Select your Export Component. In the code below, we get the JPEG Export Component.
- Specify the source for the export. This can be a Graphics Importer instance, a QuickDraw Picture, GWorld, PixMap, or a segment of compressed data described by an image description record.
- Tell the Export Component where the input is coming from. The code below uses a Graphics Importer Instance.
- Tell the Export Component the destination of the exported data. The code below uses a file.
- Specify your settings. A variety of
Get and Set functions are available to manipulate various graphics exporter settings. There are two examples in the code below.
- Explicitly tell the exporter we want low quality.
- Ask the exporter to fit the file to a given size constraint and ramp the quality accordingly.
- Do the export. The code below will write out the data and create a JPEG file.
Graphics Exporter Components Documentation can be found in the QuickTime 4 API Documentation.
Example:
OSErr DoEasyExportToJpeg()
{
SFTypeList myTypeList = {kQTFileTypeMovie, 0, 0, 0};
StandardFileReply myReply;
GraphicsImportComponent theImportComponent = NULL;
GraphicsExportComponent theExportComponent = NULL;
OSErr rc = noErr;
StandardGetFilePreview( NULL, 1, myTypeList, &myReply );
if (!myReply.sfGood) goto bail;
// 1. Open the JPEG Exporter Component.
// We'll connect an Import Component to this Export Component
// and very easily export a JPEG file.
rc = OpenADefaultComponent( GraphicsExporterComponentType, kQTFileTypeJPEG,
&theExportComponent );
if (rc) goto bail;
// 2. Find the Graphics Import Component for our file.
rc = GetGraphicsImporterForFile( &myReply.sfFile, &theImportComponent );
if (rc) goto bail;
// 3. Connect the Importer to the Exporter.
rc = GraphicsExportSetInputGraphicsImporter( theExportComponent, theImportComponent );
if (rc) goto bail;
StandardPutFile( "\pExport File As...", "\pMyJpeg.jpg", &myReply );
if (!myReply.sfGood) goto bail;
// 4. We're going to export this to a file.
rc = GraphicsExportSetOutputFile( theExportComponent, &myReply.sfFile );
if (rc) goto bail;
// 5a. Set the image compression quality.
rc = GraphicsExportSetCompressionQuality( theExportComponent, codecLowQuality );
if (rc) goto bail;
// 5b. Alternatively you could use GraphicsExportSetTargetDataSize().
// This API let's you specify the desired data size in bytes; the JPEG exporter will
// decrease the quality until it reaches the specified size or bottoms out on the
// quality setting.
//rc = GraphicsExportSetTargetDataSize( theExportComponent, 25000 ); // Max size 25k
//if (rc) goto bail;
// 6. Do the export, write the file.
rc = GraphicsExportDoExport( theExportComponent, NULL );
bail:
if ( theImportComponent != NULL )
CloseComponent( theImportComponent );
if ( theExportComponent != NULL )
CloseComponent( theExportComponent );
return rc;
}
|
Technical Q&As | Contents
Previous Question
To contact us, please use the Contact Us page.
|